package com.fancy.learncenter.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.bean.CreatClassDataBean;
import com.fancy.learncenter.net.CustomNetCallBack;
import com.fancy.learncenter.net.CustomNetSubscriber;
import com.fancy.learncenter.net.HttpMehtod;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.view.date.custom.DateTimePicker;
import com.fancy.learncenter.ui.view.date.custom.DateTimePickerDialog;
import com.fancy.learncenter.utils.NetUtil;
import com.fancy.learncenter.utils.ToastUtil;
import com.fancy.learncenter.utils.Utils;

import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Response;

public class CartoonCreatClassActivity extends BaseActivity {

    public static String NAME_KEY = "NAME_KEY";
    public static String SCHOOL_ID_KEY = "SCHOOL_ID_KEY";

    public final static int CLASS_NAME_CODE = 10003;
    public final static int CLASS_DES_CODE = 10004;


    @Bind(R.id.school_name)
    TextView schoolName;
    @Bind(R.id.classroom_name)
    TextView classroomName;
    @Bind(R.id.lesson_name)
    TextView lessonName;
    @Bind(R.id.start_time)
    TextView startTime;
    @Bind(R.id.end_time)
    TextView endTime;
    @Bind(R.id.student_num)
    EditText studentNum;
    @Bind(R.id.class_des)
    TextView classDes;
    @Bind(R.id.class_pic)
    SimpleDraweeView classPic;
    @Bind(R.id.select_pic)
    LinearLayout selectPic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cartoon_creat_class, Status.STATUS);
        ButterKnife.bind(this);
        initToolbar("创建班级");

        setRight("下一步", new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                creatClass();
            }
        });

    }

    @OnClick({R.id.select_school, R.id.select_classroom, R.id.select_lesson, R.id.class_des_ll, R.id.start_time_ll, R.id.end_time_ll, R.id.select_pic})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            case R.id.select_school:
                intent = new Intent(this, CartoonSelectSchoolActivity.class);
                startActivityForResult(intent, REQUEST_CODE);

                break;
            case R.id.select_classroom:
                intent = new Intent(this, EditTextActivity.class);
                intent.putExtra(EditTextActivity.ACTIVITY_NAME_KEY, "班级名称");
                intent.putExtra(EditTextActivity.RESULT_CODE_KEY, CLASS_NAME_CODE);
                startActivityForResult(intent, REQUEST_CODE);

                break;
            case R.id.select_lesson:
                intent = new Intent(this, CartoonSelectTextBookActivity.class);
                intent.putExtra(SCHOOL_ID_KEY, "" + schoolID);
                startActivityForResult(intent, REQUEST_CODE);
                break;
            case R.id.class_des_ll:
                intent = new Intent(this, EditTextActivity.class);
                intent.putExtra(EditTextActivity.ACTIVITY_NAME_KEY, "班级描述");
                intent.putExtra(EditTextActivity.RESULT_CODE_KEY, CLASS_DES_CODE);
                startActivityForResult(intent, REQUEST_CODE);
                break;
            case R.id.select_pic:
                intent = new Intent(this, SelectClassPicActivity.class);
                startActivityForResult(intent, REQUEST_CODE);
                break;
            case R.id.start_time_ll:

                showTimeDialog(startTime);

                break;
            case R.id.end_time_ll:

                showTimeDialog(endTime);
                break;
        }
    }


    public static int REQUEST_CODE = 10000;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE) {
            String name;
            switch (resultCode) {
                case CartoonSelectSchoolActivity.SELECT_SCHOOL_CODE:
                    name = data.getStringExtra(NAME_KEY);

                    if (!TextUtils.isEmpty(name)) {
                        schoolName.setText(name);
                    }

                    schoolID = data.getStringExtra(SCHOOL_ID_KEY);
                    break;
                case CartoonSelectTextBookActivity.SELECT_TEXT_BOOK_CODE:
                    name = data.getStringExtra(NAME_KEY);

                    if (!TextUtils.isEmpty(name)) {
                        lessonName.setText(name);
                    }

                    lessonId = data.getStringExtra(SCHOOL_ID_KEY);
                    break;
                case CLASS_NAME_CODE:
                    name = data.getStringExtra(NAME_KEY);

                    if (!TextUtils.isEmpty(name)) {
                        classroomName.setText(name);
                        className = name;
                    }

                    break;
                case CLASS_DES_CODE:
                    name = data.getStringExtra(NAME_KEY);

                    if (!TextUtils.isEmpty(name)) {
                        classDes.setText(name);

                        classDesc = name;
                    }

                    break;
                case SelectClassPicActivity.SELECT_CLASS_PIC_CODE:
                    name = data.getStringExtra(NAME_KEY);

                    if (!TextUtils.isEmpty(name)) {
                        classPic.setImageURI(name);

                    }

                    picPath = data.getStringExtra(SCHOOL_ID_KEY);

                    break;
            }

        }

    }

    String classDesc = "";
    String picPath = "2";
    String lessonId = "";
    String schoolID = "";
    String className = "";

    private void showTimeDialog(final TextView txv) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        String time = sdf.format(new Date());
        DateTimePickerDialog dialog = DateTimePickerDialog.getInstance(time, "yyyy-MM-dd HH:mm", DateTimePicker.DATE_TIME_PICKER);
        dialog.setListener(new DateTimePickerDialog.OnDateTimePickerListener() {
            @Override
            public void onDateTimePicked(long time) {
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                txv.setText(sdf.format(new Date(time)));
            }
        });
        dialog.show(getSupportFragmentManager(), DateTimePickerDialog.TAG + time);
    }


    private void creatClass() {

        if (TextUtils.isEmpty(schoolID)) {
            ToastUtil.show("请选择校区");
            return;
        }

        if (TextUtils.isEmpty(className)) {
            ToastUtil.show("请填写班级名称");
            return;
        }
        if (TextUtils.isEmpty(picPath)) {
            ToastUtil.show("请选择班级图标");
            return;
        }
        if (TextUtils.isEmpty(lessonId)) {
            ToastUtil.show("请选择班级教材");
            return;
        }
        if (startTime.getText().toString().equals("请选择开始时间")) {
            ToastUtil.show("请选择开始时间");
            return;
        }
        if (endTime.getText().toString().equals("请选择结束时间")) {
            ToastUtil.show("请选择结束时间");
            return;
        }
        if (TextUtils.isEmpty(studentNum.getText().toString())) {
            ToastUtil.show("请选择班级人数");
            return;
        }
        Map map = new HashMap();

        map.put("classStartTime", startTime.getText().toString());

        map.put("classEndTime", endTime.getText().toString());

        map.put("desc", classDesc);

        map.put("lessonId", lessonId);

        map.put("schoolInfoId", schoolID);

        map.put("name", className);

        map.put("resourceId", picPath);


        map.put("rated", studentNum.getText().toString());


        RequestBody requestBody = RequestBody.create(MediaType.parse("Content-Type, application/json"),
                new JSONObject(map).toString());

        HttpMehtod.getInstance().creatClass(requestBody, new CustomNetSubscriber(this) {
            @Override
            public void onNext(Response<ResponseBody> responseBodyResponse) {

                BaseBean<CreatClassDataBean> baseBean = Utils.fromJsonObject(NetUtil.getResResult(responseBodyResponse), CreatClassDataBean.class);
                CustomNetCallBack callBack = new CustomNetCallBack(getContext(), baseBean);
                callBack.setCallBack(new CustomNetCallBack.NetCallBack<CreatClassDataBean>() {
                    @Override
                    public void success(CreatClassDataBean dataBean) {
                        ToastUtil.show("创建成功");
                        Intent intent = new Intent(CartoonCreatClassActivity.this, CartoonShareClassActivity.class);
                        intent.putExtra("classNo", dataBean.getClassNo());
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void fail(String errorCode) {

                    }
                }).dealData();

            }
        });
    }
}
